﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using MySql.Data.MySqlClient;
using System.Collections;


namespace HRAssistant
{
    public partial class ProjectForm : Form
    {

        private Hashtable hs = new Hashtable();

        public ProjectForm()
        {
            InitializeComponent();
        }

     
        private void GetInputParam()
        {
            hs.Add("project_name",textBox1.Text.Trim());
            hs.Add("customer_name", textBox2.Text.Trim());
          
        }

        private ArrayList MakeCommand()
        {
            string command = "";
            ArrayList list = new ArrayList();
            foreach (DictionaryEntry de in hs)
            {
                if (de.Value.ToString() != "")
                {
                    command = String.Format(" {0}='{1}'", de.Key, de.Value);
                    list.Add(command);
                }
            }
            return list;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connStr = ConfigurationManager.AppSettings["MySqlConnStr"];//;MySQL 数据库连接字符串;
            string sqlCommand = "SELECT a. * , b.emp_number admin FROM (SELECT ohrm_project.project_id project_id, ohrm_project.name project_name, ohrm_customer.name customer_name,ohrm_project.description FROM ohrm_customer, ohrm_project WHERE ohrm_customer.customer_id = ohrm_project.customer_id) AS a, ohrm_project_admin b WHERE a.project_id = b.project_id";
            ArrayList list = MakeCommand();
            if (list.Count != 0)
            {
                if (list.Count != 2)
                {
                    for (int i = 0; i < list.Count - 1; i++)
                    {
                        sqlCommand += "and " + list[i].ToString();
                    }
                    sqlCommand += "and " + list[list.Count - 1];
                }
                else
                {
                    sqlCommand += "and " + list[0].ToString();
                }
            }
      
            MySqlConnection conn = new MySqlConnection(connStr);
            try
            {
                conn.Open();
            }
            catch
            {
                MessageBox.Show("打开数据库连接失败");
                return;
            }

            try
            {
                MySqlDataAdapter myadp = new MySqlDataAdapter(sqlCommand, conn);
                DataSet ds = new DataSet();
                myadp.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
                MessageBox.Show("本次查询完毕！");
            }
            catch
            {
                MessageBox.Show("执行查询失败");
                return;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
